from django.shortcuts import render
from django.http import HttpResponse
import random
from utils.tencent.sms import send_sms_single 

# Create your views here.
def hello(request):
    return HttpResponse('hello word!!!')

def send_msg(request):
    """"发送短信"""
    code = random.randrange(1000,9999)
    res = send_sms_single("18710384539",602722,[code,])
    print(res)
    return HttpResponse("短信发送成功")

from django import forms
from app01 import models
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
class RegisterModelForm(forms.ModelForm):
    mobile_phone = forms.CharField(label='手机号', validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$', '手机号格式错误'), ])
    # password = forms.CharField(label='密码',
    # widget=forms.PasswordInput(attrs={'class':"form-control",'placeholder':"请输入密码"}))
    password = forms.CharField(label='密码',
    widget=forms.PasswordInput())
    confirm_password = forms.CharField(label='确认密码',
    widget=forms.PasswordInput())
    code = forms.CharField(label='验证码')
    class Meta:
        model = models.UserInfo
        #fields = "__all__"
        fields = ['username','password','confirm_password','email','mobile_phone','code']

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        for name,filed in self.fields.items():
            filed.widget.attrs['class'] = "form-control"
            filed.widget.attrs['placeholder'] = '请输入%s'%(filed.label,)


def register(request):
    form = RegisterModelForm()
    return render(request,'app01/register.html',{"form":form})